diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-08 08:57:08 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-08 08:57:08 +0200 |
| commit | 4b2b964283075712de98743720ff3e31370ddc8e (patch) | |
| tree | 7200e83869dbd42d5f20318a07a6ca328e5ac59f /src/routes/projects/[projectId] | |
| parent | 992e40835af1600a0961ec2454fb78ff43aec3c6 (diff) | |
Deduplicate routine actions and badges across project routes
- Share edit/complete/archive form actions in routine-actions.ts; route
servers become thin wrappers (strictest guard semantics in one place)
- Move parseFrequencyDays into validation.ts
- Extract ScoreBadge/StatusBadge components; use on project pages and
the home page instead of duplicated scoreClass/statusClass helpers
- Fix archiveProject calling the routine archive with the project id
Diffstat (limited to 'src/routes/projects/[projectId]')
| -rw-r--r-- | src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts | 69 | ||||
| -rw-r--r-- | src/routes/projects/[projectId]/routine/[routineId]/+page.svelte | 27 |
2 files changed, 18 insertions, 78 deletions
diff --git a/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts b/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts index f2b6551..df4c2cc 100644 --- a/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts +++ b/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts @@ -1,26 +1,14 @@ -import { error, fail } from '@sveltejs/kit'; +import { error } from '@sveltejs/kit'; import { getById } from '$lib/server/services/projects'; +import { loadCurrent, loadCompletions } from '$lib/server/services/routines'; import { - edit, - complete, - loadCurrent, - loadCompletions, -} from '$lib/server/services/routines'; -import { numericId, parseFormString } from '$lib/server/services/validation'; -import { - parseIntervalToDays, - computeRoutineState, -} from '$lib/shared/routine-metrics'; + completeRoutineAction, + editRoutineAction, +} from '$lib/server/services/routine-actions'; +import { numericId } from '$lib/server/services/validation'; +import { computeRoutineState } from '$lib/shared/routine-metrics'; import type { Actions, PageServerLoad } from './$types'; -function parseFrequencyDays(value: FormDataEntryValue | null): number { - const days = parseIntervalToDays(value?.toString() ?? ''); - if (days === null) { - throw new Error('Invalid interval'); - } - return days; -} - export const load: PageServerLoad = async ({ params }) => { const projectId = numericId(params.projectId); const routineId = numericId(params.routineId); @@ -49,49 +37,14 @@ export const load: PageServerLoad = async ({ params }) => { export const actions: Actions = { edit: async ({ request, params }) => { - const current = await loadCurrent(numericId(params.routineId)); - if (!current) { - return fail(400, { error: 'Routine introuvable' }); - } - if (current.archived) { - return fail(400, { error: 'Routine archivée' }); - } const data = await request.formData(); - const name = parseFormString(data.get('name')); - if (!name) { - return fail(400, { error: 'Le nom est requis' }); - } - const description = data.get('description')?.toString() ?? ''; - try { - await edit(numericId(params.routineId), { - name, - frequencyDays: parseFrequencyDays(data.get('frequencyDays')), - description, - }); - } catch { - return fail(400, { error: 'Fréquence invalide' }); - } - return { success: true, type: 'edit' as const }; + return editRoutineAction(data, numericId(params.routineId), { + type: 'edit', + }); }, complete: async ({ request, params }) => { - const current = await loadCurrent(numericId(params.routineId)); - if (!current) { - return fail(400, { error: 'Routine introuvable' }); - } - if (current.archived) { - return fail(400, { error: 'Routine archivée' }); - } const data = await request.formData(); - try { - await complete( - numericId(params.routineId), - parseFormString(data.get('note')), - new Date().toISOString(), - ); - } catch { - return fail(400, { error: 'Routine introuvable' }); - } - return { success: true }; + return completeRoutineAction(data, numericId(params.routineId)); }, }; diff --git a/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte b/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte index 5f57de4..19713ae 100644 --- a/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte +++ b/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte @@ -1,11 +1,11 @@ <script lang="ts"> import { enhance } from '$app/forms'; + import ScoreBadge from '$lib/components/ScoreBadge.svelte'; + import StatusBadge from '$lib/components/StatusBadge.svelte'; import { formatFrequency, parseIntervalToDays, - STATUS_LABELS, computeHealthSeries, - type RoutineState, } from '$lib/shared/routine-metrics'; let { data, form } = $props(); @@ -74,19 +74,6 @@ .filter((m): m is { x: number; y: number } => m !== null); return { points, grid, markers }; }); - - function scoreClass(score: number): string { - if (score >= 80) return 'score bg-success'; - if (score >= 50) return 'score bg-accent'; - return 'score bg-danger'; - } - - function statusClass(status: RoutineState['status']): string { - if (status === 'ok') return 'status bg-success text-white'; - if (status === 'due') return 'status bg-accent-soft text-accent'; - if (status === 'late') return 'status bg-accent text-white'; - return 'status bg-danger text-white'; - } </script> <main> @@ -96,7 +83,7 @@ <header class="page-header"> <h1>{data.routine.name}</h1> - <span class={scoreClass(data.state.health)}>{data.state.health}</span> + <ScoreBadge score={data.state.health} /> </header> {#if data.routine.archived} @@ -195,10 +182,10 @@ {/each} </svg> <div class="chart-footer"> - <span class={statusClass(data.state.status)}> - {STATUS_LABELS[data.state.status]} - {#if data.state.overdueDays > 0}({data.state.overdueDays} j){/if} - </span> + <StatusBadge + status={data.state.status} + overdueDays={data.state.overdueDays} + /> <span class="chart-meta"> {formatFrequency(data.routine.frequencyDays)} {#if data.state.lastDoneAt} |
